Imports System.Collections.Generic

Namespace CardGameFramework

    Public Class Hand
        ' Creates a list of cards
        Protected _cardsProp As New List(Of Card)
        Private _lotrump As Integer
        Private _hitrump As Integer
        Private _tarottrump As Integer
        Private _seq4 As Integer
        Private _seq7 As Integer
        Private _seq10 As Integer
        Public Property Cards() As List(Of Card)
            Get
                Return _cardsProp
            End Get
            Set(ByVal value As List(Of Card))
                _cardsProp = value
            End Set
        End Property

        Public ReadOnly Property NumCards() As Integer
            Get
                Return _cardsProp.Count
            End Get
        End Property
        Public Property LoTrump As Integer
            Get
                Return _lotrump
            End Get
            Set(ByVal value As Integer)
                _lotrump = value
            End Set
        End Property
        Public Property HiTrump As Integer
            Get
                Return _hitrump
            End Get
            Set(ByVal value As Integer)
                _hitrump = value
            End Set
        End Property
        Public Property TarotTrump As Integer
            Get
                Return _tarottrump
            End Get
            Set(ByVal value As Integer)
                _tarottrump = value
            End Set
        End Property
        Public Property Seq4 As Integer
            Get
                Return _seq4
            End Get
            Set(ByVal value As Integer)
                _seq4 = value
            End Set
        End Property
        Public Property Seq7 As Integer
            Get
                Return _seq7
            End Get
            Set(ByVal value As Integer)
                _seq7 = value
            End Set
        End Property
        Public Property Seq10 As Integer
            Get
                Return _seq10
            End Get
            Set(ByVal value As Integer)
                _seq10 = value
            End Set
        End Property

        ''' <summary>
        ''' Checks to see if the hand contains a card
        ''' </summary>
        ''' <param name="item"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ContainsCard(ByVal mysuit As Suit, ByVal item As FaceValue) As Boolean
            For Each c As Card In _cardsProp
                If c.FaceVal = item And c.Suit = mysuit Then
                    Return True
                End If
            Next
            Return False
        End Function
        Public Function ContainsSuit(ByVal mysuit As Suit) As Boolean
            For Each c As Card In _cardsProp
                If c.Suit = mysuit Then
                    Return True
                End If
            Next
            Return False
        End Function
        Public Function CountSuit(ByVal mysuit As Suit) As Integer
            Dim counts As Integer = 0
            For Each c As Card In _cardsProp
                If c.Suit = mysuit Then
                    counts += 1
                End If
            Next
            Return counts
        End Function
    End Class
    ''' <summary>
    ''' This class is game-specific.  Creates a hand that inherits from class hand
    ''' </summary>
    ''' <remarks></remarks>
    Public Class TarotHand
        Inherits Hand
      
        Public Sub SortHand(ByVal aHand As TarotHand)
            Dim cval(23) As Integer
            Dim temp As Card
            Dim tempi As Integer
            For i As Integer = 0 To 23
                cval(i) = aHand.Cards(i).Suit + aHand.Cards(i).PlayValue
            Next
            For j = 1 To 24
                For i As Integer = 1 To 23
                    If cval(i - 1) < cval(i) Then
                        temp = aHand.Cards(i - 1)
                        tempi = cval(i - 1)
                        aHand.Cards(i - 1) = aHand.Cards(i)
                        cval(i - 1) = cval(i)
                        aHand.Cards(i) = temp
                        cval(i) = tempi
                    End If
                Next
            Next
        End Sub

        ''' <remarks></remarks>
        Public Sub GetMeld(ByVal otherHand As TarotHand)
            Dim meld As Integer = 0
            Dim numHiTrump As Integer = 0
            Dim numLoTrump As Integer = 0
            Dim numTarotTrumps As Integer = 0
            Dim seq4 As Integer = 0
            Dim seq7 As Integer = 0
            Dim seq10 As Integer = 0

            For Each c As Card In otherHand.Cards
                If c.Suit = Suit.Trumps Then
                    If c.FaceVal > 1600 Then 'High Trumps t17 - t21
                        numHiTrump += 1
                    End If
                    If c.FaceVal < 6 And c.FaceVal > 0 Then 'low trumps t1-t5
                        numLoTrump += 1
                    End If
                    If c.FaceVal = FaceValue.Fool Or c.FaceVal = FaceValue.TwentyOne Or c.FaceVal = FaceValue.Ace Then 'tarot trumps
                        numTarotTrumps += 1
                    End If
                End If
                If c.FaceVal = FaceValue.King Then
                    numTarotTrumps += 1
                End If
            Next
            Select Case numHiTrump
                Case Is < 3
                    otherHand.HiTrump = 0
                Case Is = 3
                    meld += 5
                    otherHand.HiTrump = 5
                Case Is = 4
                    meld += 10
                    otherHand.HiTrump = 10
                Case Is = 5
                    meld += 15
                    otherHand.HiTrump = 15
            End Select
            Select Case numLoTrump
                Case Is < 3
                    otherHand.LoTrump = 0
                Case Is = 3
                    meld += 5
                    otherHand.LoTrump = 5
                Case Is = 4
                    meld += 10
                    otherHand.LoTrump = 10
                Case Is = 5
                    meld += 15
                    otherHand.LoTrump = 15
            End Select
            Select Case numTarotTrumps
                Case Is < 3
                    otherHand.TarotTrump = 0
                Case Is > 2, Is < 6
                    meld += 15
                    otherHand.TarotTrump = 15
                Case Is > 5
                    meld += 30
                    otherHand.TarotTrump = 30
            End Select

            'look for sequences in suits
            'sort hand by suits
            SortHand(otherHand)
            Dim hs As String = ""
            For i As Integer = 1 To 23
                If otherHand.Cards(i).Suit = otherHand.Cards(i - 1).Suit Then
                    If otherHand.Cards(i).PlayValue = otherHand.Cards(i - 1).PlayValue - 1 Then
                        hs += "a"
                    Else
                        hs += "/"
                    End If
                End If
            Next
            Dim chs() As String = Split(hs, "/", -1)
            For Each s As String In chs
                If s.Length > 8 Then
                    seq10 += 1
                End If
                If s.Length > 5 And s.Length < 9 Then
                    seq7 += 1
                End If
                If s.Length > 2 And s.Length < 6 Then
                    seq4 += 1
                End If
            Next
            otherHand.Seq10 = 15 * seq10
            meld += 15 * seq10
            otherHand.Seq7 = 10 * seq7
            meld += 10 * seq7
            otherHand.Seq4 = 5 * seq4
            meld += 5 * seq4
        End Sub
    End Class
End Namespace